package com.itheima.dao;

import com.itheima.domain.Account;
import com.itheima.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import javax.xml.crypto.Data;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@Repository
public class AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private DataSource dataSource;
    public void add(int id, double money){

        try {
            /*Connection connection = DataSourceUtils.getConnection(dataSource);
            //Connection connection = DataSourceUtil.getConnection(dataSource);

            PreparedStatement preparedStatement =connection.prepareStatement("update account set money=money+? where id=?");

            preparedStatement.setDouble(1,money);
            preparedStatement.setInt(2,id);
            preparedStatement.execute();


            preparedStatement.close();*/

            String sql="update account set money=money+? where id=?";
            jdbcTemplate.update(
                    sql,
                    money,id
            );







        } catch (Exception e) {

            throw new RuntimeException(e);
        }


    }
    public void sub(int id,double money){

        try{
            /*Connection connection = DataSourceUtils.getConnection(dataSource);

            PreparedStatement preparedStatement = connection.prepareStatement("update account set money=money-? where id=?");

            preparedStatement.setDouble(1,money);
            preparedStatement.setInt(2,id);
            preparedStatement.execute();

            preparedStatement.close();*/

            /*QueryRunner queryRunner = new QueryRunner();

            String sql="............";
            queryRunner.update(
                    sql,
                    money,id
            );
*/

            String sql="update account set money=money-? where id=?";
            jdbcTemplate.update(
                    sql,
                    money,id
            );


        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }


    public void save(Account account) {
        System.out.println("完成了插入功能");
    }

    public void delete(int id) {
        System.out.println("完成了删除功能");
    }

    public Account findById(int id) {

        String sql="select * from  account where id=?";

        /*List<Account> list = jdbcTemplate.query(
                sql,
                new BeanPropertyRowMapper<>(Account.class),
                id
        );*/

        Account account = jdbcTemplate.queryForObject(
                sql,
                new BeanPropertyRowMapper<>(Account.class),
                id
        );

        return account;
    }
}
